package cn.micro.system.manage.service.impl;

import cn.micro.system.manage.common.enums.EmployeeStatusEnum;
import cn.micro.system.manage.common.enums.UserStatusEnum;
import cn.micro.system.manage.entity.SysDimDeptRole;
import cn.micro.system.manage.entity.SysDimEmployee;
import cn.micro.system.manage.entity.SysDimUser;
import cn.micro.system.manage.entity.SysDimUserRole;
import cn.micro.system.manage.entity.dto.SysDimEmployeeSearch;
import cn.micro.system.manage.entity.dto.UpdateStatusDTO;
import cn.micro.system.manage.entity.vo.SysDimEmployeeVO;
import cn.micro.system.manage.mapper.SysDimDeptRoleMapper;
import cn.micro.system.manage.mapper.SysDimEmployeeMapper;
import cn.micro.system.manage.mapper.SysDimUserMapper;
import cn.micro.system.manage.mapper.SysDimUserRoleMapper;
import cn.micro.system.manage.service.SysDimEmployeeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 员工信息表 服务实现类
 * </p>
 *
 * @author wr
 * @since 2024-01-01
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class SysDimEmployeeServiceImpl extends ServiceImpl<SysDimEmployeeMapper, SysDimEmployee> implements SysDimEmployeeService {

    private final SysDimDeptRoleMapper sysDimDeptRoleMapper;

    private final SysDimUserMapper sysDimUserMapper;

    private final SysDimUserRoleMapper sysDimUserRoleMapper;

    @Override
    @Transactional
    public boolean saveEmployee(SysDimEmployee sysDimEmployee) {
        LambdaQueryWrapper<SysDimDeptRole> deptRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        deptRoleLambdaQueryWrapper.eq(SysDimDeptRole::getDeptId,sysDimEmployee.getDeptId());
        List<SysDimDeptRole> deptRoleList = sysDimDeptRoleMapper.selectList(deptRoleLambdaQueryWrapper);
        //新建用户信息
        if (deptRoleList!= null && deptRoleList.size() > 0){
            SysDimUser sysDimUser = new SysDimUser();
            sysDimUser.setUsername(sysDimEmployee.getCode());
            sysDimUser.setPassword(sysDimEmployee.getIdCard().substring(sysDimEmployee.getIdCard().length()-6));
            sysDimUser.setEmail(sysDimEmployee.getEmail());
            sysDimUser.setPhone(sysDimEmployee.getPhone());
            sysDimUser.setName(sysDimEmployee.getName());
            sysDimUser.setDeptId(sysDimEmployee.getDeptId());
            sysDimUser.setPositionId(sysDimEmployee.getPositionId());
            sysDimUserMapper.insert(sysDimUser);
            for (SysDimDeptRole sysDimDeptRole : deptRoleList) {
                sysDimUserRoleMapper.insert(new SysDimUserRole().setUserId(Long.parseLong(sysDimUser.getId())).setRoleId(sysDimDeptRole.getRoleId()));
            }
            sysDimEmployee.setUserId(Long.parseLong(sysDimUser.getId()));
        }
        return this.save(sysDimEmployee);
    }

    @Override
    @Transactional
    public void removeEmployee(Long id) {
        SysDimEmployee sysDimEmployee = this.getById(id);
        if (sysDimEmployee == null){
            throw new RuntimeException("员工信息不存在");
        }
        //删除角色与用户关系
        sysDimUserRoleMapper.delete(new LambdaQueryWrapper<SysDimUserRole>().eq(SysDimUserRole::getUserId,sysDimEmployee.getUserId()));
        //删除用户信息
        sysDimUserMapper.deleteById(sysDimEmployee.getUserId());
        //删除员工信息
        this.removeById(id);
    }

    @Override
    @Transactional
    public boolean updateStatus(UpdateStatusDTO dto) {
        SysDimEmployee employee = this.getById(dto.getId());
        if (employee == null){
            throw new RuntimeException("员工信息不存在");
        }
        employee.setStatus(dto.getStatus());
        boolean result = this.updateById(employee);
        if (result){
            SysDimUser user = new SysDimUser();
            user.setId(String.valueOf(employee.getUserId()));
            if (Objects.equals(dto.getStatus(), EmployeeStatusEnum.EMPLOYED.getValue())){
                user.setStatus(UserStatusEnum.NORMAL.getValue());
            }else if (Objects.equals(dto.getStatus(), EmployeeStatusEnum.RESIGN.getValue())){
                user.setStatus(UserStatusEnum.DISABLED.getValue());
            }
            result = sysDimUserMapper.updateById(user) > 0;
        }
        return result;
    }

    @Override
    public List<SysDimEmployeeVO> listByPage(int page, int size, SysDimEmployeeSearch search) {
        PageHelper.startPage(page,size);
        return this.baseMapper.queryByPage(search);
    }

    @Override
    public List<SysDimEmployee> getCode(SysDimEmployee employee) {
        LambdaQueryWrapper<SysDimEmployee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(employee.getCode()),SysDimEmployee::getCode,employee.getCode())
                .ne(StringUtils.isNotEmpty(employee.getId()),SysDimEmployee::getId,employee.getId());
        return this.baseMapper.selectList(queryWrapper);
    }
}
